package com.gugee.ins.common.dal.service.blogger.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.blogger.InsRelBloggerFollowingMapper;
import com.gugee.ins.common.dal.service.blogger.InsRelBloggerFollowingDao;
import com.gugee.ins.common.model.mysql.blogger.InsRelBloggerFollowing;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * InsRelBloggerFollowingDao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsRelBloggerFollowingDaoImpl extends ServiceImpl<InsRelBloggerFollowingMapper, InsRelBloggerFollowing> implements InsRelBloggerFollowingDao {

    /**
     * 批量插入 博主和关注人的关联信息
     * @param insRelBloggerFollowings
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertByUid(List<InsRelBloggerFollowing> insRelBloggerFollowings) {
        try{
            this.saveBatch(insRelBloggerFollowings);
        }catch (Exception e){
            log.debug("batch insert Active Follower Following failed ,try insert one by one, msg : {} ",e.getMessage());
            insRelBloggerFollowings.forEach(insRelBloggerFollowing -> {
                try{
                    baseMapper.insert(insRelBloggerFollowing);
                }catch (Exception ex){
                    log.info("insert Active Follower Following failed ,uid : {} ,followingUid : {}, msg : {} "
                            ,insRelBloggerFollowing.getUid(),insRelBloggerFollowing.getFUid(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 删除博主的活跃粉丝关系信息
     * @param uid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByUid(long uid) {
        Map<String,Object> deleteMap = new HashMap<>(1);
        deleteMap.put("uid",uid);
        baseMapper.deleteByMap(deleteMap);
    }
}
